// 消息显示时间
var msgShowTime = 1500
//警告消息图标
var icoHint = 0
//成功信息图标
var icoSuc = 1
//错误信息图标
var icoErr = 2

// 允许上传的单个文件大小
var uploadFileSize = $('#fileSize').val()
//允许上传的图片格式
var uploadImgExt = $('#imgExt').val()

function ajaxRequest(url,data,success_callback,error_callback,type="POST") {
    if (error_callback == null || error_callback == '' || error_callback == 'undefined'){
        error_callback = function () {
            layer.msg('系统繁忙，请重试',{icon:icoHint,time:msgShowTime})
        }
    }
    return $.ajax({
        type:type,
        url:url,
        data:data,
        dataType:"JSON",
        success:success_callback,
        error:error_callback
    });
}

/**
 * 清理系统缓存
 * @param url
 */
function clearAll(obj) {
    var url = obj.getAttribute('data-GetUrl');
    layer.confirm('确定清理系统缓存？', {icon: 3, title:''},
        function(){
            $.get(url,function(info){
                if(info.code === 1){
                    setTimeout(function () {location.href = info.url;}, msgShowTime);  //清除成功1秒后刷新页面，如果不需要刷新可以注释掉此行。
                }
                //layer.msg(info.msg,{icon:1});
                layer.msg(info.msg);
            });
        },
        function(){});
}

/**
 * 弹出提示/警告消息
 * @param msg
 * @param obj 获取焦点的dom节点的id
 */
function hintMsg(msg,obj){
    if (obj !== '') {
        $('#'+obj).focus()
    }
    layer.msg(msg,{icon: icoHint,time: msgShowTime})
}

/**
 * 弹出成功消息
 * @param msg
 * @param callback
 */
function sucMsg(msg,callback){
    if (callback == null || callback == '' || callback == 'undefined') {
        layer.msg(msg,{icon:icoSuc,time:msgShowTime})
    }else {
        layer.msg(msg,{icon:icoSuc,time:msgShowTime},callback)
    }
}

/**
 * 弹出错误消息
 * @param msg
 * @param callback
 */
function errMsg(msg,callback){
    if (callback == null || callback == '' || callback == 'undefined') {
        layer.msg(msg,{icon:icoErr,time:msgShowTime})
    }else {
        layer.msg(msg,{icon:icoErr,time:msgShowTime},callback)
    }
}

/**
 * 检查变量是否为空
 * @param value
 * @returns {boolean}
 */
function emptyCheck(value){
    if (value == '' || value == null || value == 'undefined') {
        return true
    }
    return false
}

/**
 * 修改记录状态值
 * @param url
 * @param id
 * @param type 1:show 2:hidden
 * @param field 1:status 2:is_recommended 3:is_hot
 * @param tip
 * @param listObj
 * @param elem
 * @param page
 */
function changeStatus(url,id,type,field,tip,listObj,elem,page){
    layer.confirm(tip,{title:false,btn:['确定','取消'],cancel:function (index) {
            listObj.reload(elem,{page:page})
        }
    },function (index) {
        // var commonShade = layer.load(1,{shade:[0.5,"#FFF"]})
        ajaxRequest(url,{id:id,type:type,field:field},function (res) {
            layer.close(index)
            if (res.code == 'suc') {
                sucMsg(res.msg)
            } else {
                errMsg(res.msg)
            }
        })
        listObj.reload(elem,{page: page})
        layer.close()
    },function () {
        listObj.reload(elem,{page: page})
    })
}

/**
 * 判断两个对象是否一致
 * @param obj1
 * @param obj2
 * @returns {boolean}
 */
function isObjEqual(obj1, obj2) {
    var props1 = Object.getOwnPropertyNames(obj1);
    var props2 = Object.getOwnPropertyNames(obj2);

    if (props1.length != props2.length){
        return false;
    }
    for (var i = 0, max = props1.length; i < max; i++){
        var propName = props1[i];
        if (obj1[propName] !== obj2[propName]) {
            return false;
        }
    }
    return true;
}

/**
 * 操作穿梭框数据
 * @param action 0:添加；1：移除
 * @param obj 添加/移除的数据
 * @param target 目标数组
 * @returns {*}
 */
function handleTransferData(action, obj, target) {
    if (action == 0) { //新增
        if (obj.length == 1) {
            target.push(obj[0].value)
        } else {
            for (let i = 0; i < obj.length; i++) {
                target.push(obj[i].value)
            }
        }
    } else { //移除
        if (obj.length == 1){
            target.splice($.inArray(obj[0].value.toString(),target),1)
        }else {
            for (let i = 0; i < obj.length; i++) {
                target.splice($.inArray(obj[i].value.toString(),target),1)
            }
        }
    }

    return target
}

$(document).on("mouseenter mouseleave",".file-items",function (event) {
    if (event.type === "mouseenter") {
        //鼠标悬浮
        $(this).children(".handle").fadeIn("fast");
    }else if(event.type === "mouseleave"){
        //鼠标离开
        $(this).children(".handle").hide();
    }
})